﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Projektarbete.AI
{
    class MerchantInventory
    {
        public enum SortOptions { Name, Price, Weight, Size };

        public LinkedList<Item> contents;

        int count;
        public int Count { get { return count; } }

        int tempInt;

        /// <summary>
        /// Creates a new inventory for a merchant
        /// </summary>
        public MerchantInventory()
        {
            contents = new LinkedList<Item>();
            count = contents.Count;
        }

        /// <summary>
        /// Creates a new inventory for a merchant
        /// </summary>
        /// <param name="items">Items that the inventory should contain from start</param>
        public MerchantInventory(Item[] items)
        {
            contents = new LinkedList<Item>(items);
            count = contents.Count;
        }

        /// <summary>
        /// Checks if the bag contains an item
        /// </summary>
        /// <param name="item">Item to check</param>
        /// <returns>Häst</returns>
        public bool Contains(Item item)
        {
            return contents.Contains(item);
        }

        /// <summary>
        /// Checks if the bag contains an item
        /// </summary>
        /// <param name="item">Item to check</param>
        /// <param name="amount">Amount</param>
        /// <returns>Horses</returns>
        public bool Contains(Item item, int amount)
        {
            tempInt = 0;

            foreach (Item currentItem in contents)
                if (currentItem.Equals(item))
                    tempInt++;

            return tempInt >= amount;
        }

        /// <summary>
        /// Returns the amount of the specified item the bag contains
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int Amount(Item item)
        {
            tempInt = 0;

            //TODO: currenItem.name == item.name ???
            foreach (Item currentItem in contents)
                if (currentItem == item)
                    tempInt++;

            return tempInt;
        }

        /// <summary>
        /// Adds an item to the backpack if it doesn't exceed the max weight/size. Return true if sucessfully added
        /// </summary>
        /// <param name="item">Item to add</param>
        /// <returns>If sucessfully added</returns>
        public void AddItem(Item item)
        {
            contents.AddLast(item);
            count = contents.Count;
        }

        /// <summary>
        /// Removes a single item from the backpack
        /// </summary>
        /// <param name="item">Item to remove</param>
        public void RemoveItem(Item item)
        {
            if (contents.Contains(item))
                contents.Remove(item);
            count = contents.Count;
        }

        /// <summary>
        /// Removes an (or more) items from the backpack
        /// </summary>
        /// <param name="item">Item to remove</param>
        /// <param name="amount">Amount to remove</param>
        public void RemoveItem(Item item, int amount)
        {
            int available = 0;

            foreach (Item currentItem in contents)
                if (currentItem.Equals(item))
                    available++;


            if (available >= amount)
                for (int x = 0; x < amount; x++)
                    contents.Remove(item);

            count = contents.Count;
        }

        /// <summary>
        /// Removes every instance of the selected item from the backpack
        /// </summary>
        /// <param name="item">Item to remove</param>
        public void RemoveAllOfItem(Item item)
        {
            int available = 0;

            foreach (Item currentItem in contents)
                if (currentItem.Equals(item))
                    available++;

            for (int x = 0; x < available; x++)
                contents.Remove(item);

            count = contents.Count;
        }

        /// <summary>
        /// Gets the inventory as an array (for viewing in popup)
        /// </summary>
        /// <param name="xDimensions"></param>
        /// <returns></returns>
        public Item[][] GetArray(int xDimensions)
        {
            //int size = (int)Math.Ceiling((double)(contents.Count / xDimensions));

            Item[][] returnArray = new Item[8][];

            for (int y = 0; y < 8; y++)
            {
                returnArray[y] = new Item[16];

                for (int x = 0; x < 16; x++)
                {
                    try
                    {
                        returnArray[y][x] = contents.ElementAt(x + (xDimensions * y));
                    }
                    catch (ArgumentOutOfRangeException)
                    {
                        returnArray[y][x] = new ItemPresets.EmptySlot();
                    }
                }
            }

            return returnArray;
        }

        //TODO: SORT BACKPACK
    }
}
